package com.tg.appcommon.android;

import android.app.Activity;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.view.View;
import androidx.annotation.NonNull;
import com.appbase.custom.constant.UserProtocolConstants;
import com.module.appcommon.R;
import com.tg.appcommon.business.ILoginModule;
import com.tg.appcommon.router.TGBusiness;


public class ClickableSpanHelper {

    public static SpannableString getRegisterClickableSpan(String text, final Activity activity) {
        SpannableString spanableInfo = new SpannableString(text);
        class ClickableAgreementtv extends ClickableSpan implements View.OnClickListener {
            public ClickableAgreementtv() {}

            @Override
            public void onClick(View v) {
                ILoginModule iLoginModule = TGBusiness.getLoginModule();
                iLoginModule.getAgreementForWebView(activity, UserProtocolConstants.USER_PROTOCOL);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ResourcesUtil.getResources().getColor(R.color.global_customize_fixed_color_link));       //设置文件颜色
                ds.setUnderlineText(true);      //设置下划线
            }
        }

        class RegisterClickablePrivacy extends ClickableSpan implements View.OnClickListener {
            public RegisterClickablePrivacy() {}

            @Override
            public void onClick(View v) {
                TGBusiness.getLoginModule().register(activity);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ResourcesUtil.getResources().getColor(R.color.global_customize_fixed_color_link));       //设置文件颜色
                ds.setUnderlineText(true);      //设置下划线
            }
        }


        String text1 = ResourcesUtil.getString(R.string.register_user);
        int pos1 = text.indexOf(text1);


        if (pos1>0){
            spanableInfo.setSpan(new RegisterClickablePrivacy(), pos1, pos1 + text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spanableInfo.setSpan(new ForegroundColorSpan(ResourcesUtil.getResources().getColor(R.color.global_customize_fixed_color_link)), pos1, pos1 + text1.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        }


        return spanableInfo;
    }

     public static SpannableString getClickableSpan(String text, final Activity activity) {
        SpannableString spanableInfo = new SpannableString(text);
        class ClickableAgreementtv extends ClickableSpan implements View.OnClickListener {
            public ClickableAgreementtv() {}

            @Override
            public void onClick(View v) {
                ILoginModule iLoginModule = TGBusiness.getLoginModule();
                iLoginModule.getAgreementForWebView(activity, UserProtocolConstants.USER_PROTOCOL);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ResourcesUtil.getResources().getColor(R.color.global_customize_fixed_color_link));       //设置文件颜色
                ds.setUnderlineText(true);      //设置下划线
            }
        }

        class ClickablePrivacy extends ClickableSpan implements View.OnClickListener {
            public ClickablePrivacy() {}

            @Override
            public void onClick(View v) {
                ILoginModule iLoginModule = TGBusiness.getLoginModule();
                iLoginModule.getAgreementForWebView(activity, UserProtocolConstants.PRIVACY);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ResourcesUtil.getResources().getColor(R.color.global_customize_fixed_color_link));       //设置文件颜色
                ds.setUnderlineText(true);      //设置下划线
            }
        }


        String text1 = ResourcesUtil.getString(R.string.user_agreement);
        String text2 = ResourcesUtil.getString(R.string.privacy_policy);
        int pos1 = text.indexOf(text1);
        int pos2 = text.indexOf(text2);

         if (pos1>0){
             spanableInfo.setSpan(new ClickableAgreementtv(), pos1, pos1 + text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
             spanableInfo.setSpan(new ForegroundColorSpan(ResourcesUtil.getResources().getColor(R.color.global_customize_fixed_color_link)), pos1, pos1 + text1.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

         }
         if (pos2>0) {
             spanableInfo.setSpan(new ClickablePrivacy(), pos2, pos2 + text2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
             spanableInfo.setSpan(new ForegroundColorSpan(ResourcesUtil.getResources().getColor(R.color.global_customize_fixed_color_link)), pos2, pos2 + text2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
         }

        return spanableInfo;
    }


    public static SpannableString getClickableSpanForMessage(String text, final View.OnClickListener listener) {
        SpannableString spanableInfo = new SpannableString(text);
        class ClickableMessage extends ClickableSpan implements View.OnClickListener {
            public ClickableMessage() {}

            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.onClick(v);
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ResourcesUtil.getResources().getColor(R.color.global_customize_fixed_color_link));       //设置文件颜色
                ds.setUnderlineText(false);
            }
        }

        String text1 = ResourcesUtil.getString(R.string.text_message_tip_click);
        int pos = text.indexOf(text1);
        TGLog.d("pos1 == " + pos);
        if (pos > 0){
            spanableInfo.setSpan(new ClickableMessage(), pos, pos + text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spanableInfo.setSpan(new ForegroundColorSpan(ResourcesUtil.getResources().getColor(R.color.global_customize_fixed_color_link)), pos, pos + text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        return spanableInfo;
    }

    public static CharSequence getClickableHtml(final Activity activity, Spanned spannedHtml) {
        SpannableStringBuilder clickableHtmlBuilder = new SpannableStringBuilder(spannedHtml);
        URLSpan[] urls = clickableHtmlBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);
        for (final URLSpan span : urls){
            setLinkClickable(activity, clickableHtmlBuilder, span);
        }
        return clickableHtmlBuilder;
    }

    public static void setLinkClickable(final Activity activity, final SpannableStringBuilder clickableHtmlBuilder, final URLSpan urlSpan) {
        int start = clickableHtmlBuilder.getSpanStart(urlSpan);
        int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
        int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                String originUrl = urlSpan.getURL();//获取url地址
                TGLog.d("showProtocol1 "+ originUrl);
                ILoginModule iLoginModule = TGBusiness.getLoginModule();
                if (originUrl.contains("user")){
                    iLoginModule.getAgreementForWebView(activity, UserProtocolConstants.USER_PROTOCOL);

                }else if (originUrl.contains("privacy")){
                    iLoginModule.getAgreementForWebView(activity, UserProtocolConstants.PRIVACY);
                }

            }
        };
        clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
    }

}
